package com.jxdinfo.hussar.cloud.module.example.aqmk.attendancedevice1.service.impl;

import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.cloud.module.example.aqmk.attendancedevice1.model.AttendanceDevice1;
import com.jxdinfo.hussar.cloud.module.example.aqmk.attendancedevice1.service.AttendanceDevice1Service;
import com.jxdinfo.hussar.cloud.module.example.aqmk.attendancedevice1.dao.AttendanceDevice1Mapper;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.util.Map;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import java.util.HashMap;
import com.jxdinfo.hussar.support.mp.base.query.generator.superquery.SuperQueryGenerator;
import com.jxdinfo.hussar.cloud.module.example.aqmk.attendancedevice1.vo.AttendanceDevice1PageVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;

/**
 * 考勤设备表1服务实现类
 */
@Service("aqmk.attendancedevice1.AttendanceDevice1ServiceImpl")
@HussarDs("master")
public class AttendanceDevice1ServiceImpl extends HussarServiceImpl<AttendanceDevice1Mapper, AttendanceDevice1> implements AttendanceDevice1Service {

    private static final Logger logger = LoggerFactory.getLogger(AttendanceDevice1ServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Override
    public ApiResponse<AttendanceDevice1PageVO> hussarQueryPage(Page<AttendanceDevice1> page) {
        System.out.println(123);

        try {
            Page<AttendanceDevice1> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            AttendanceDevice1PageVO attendanceDevice1PageVO = new AttendanceDevice1PageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<AttendanceDevice1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new AttendanceDevice1(), parameterMap);
            List<AttendanceDevice1> list = this.page(pager, queryWrapper).getRecords();
            attendanceDevice1PageVO.setData(list);
            attendanceDevice1PageVO.setCount(pager.getTotal());
            attendanceDevice1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(attendanceDevice1PageVO);
        } catch (Exception e) {
            throw new HussarException("表格查询失败");
        }
    }

    @Override
    public ApiResponse<AttendanceDevice1PageVO> hussarQueryPage_order_custom(Page<AttendanceDevice1> page) {
        try {
            Page<AttendanceDevice1> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            AttendanceDevice1PageVO attendanceDevice1PageVO = new AttendanceDevice1PageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<AttendanceDevice1> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new AttendanceDevice1(), parameterMap);
            List<AttendanceDevice1> list = this.page(pager, queryWrapper).getRecords();
            attendanceDevice1PageVO.setData(list);
            attendanceDevice1PageVO.setCount(pager.getTotal());
            attendanceDevice1PageVO.setCode(RETURN_CODE);
            return ApiResponse.success(attendanceDevice1PageVO);
        } catch (Exception e) {
            throw new HussarException("表格查询失败");
        }
    }
}
